<template>
  <div class="add-main-box">
    <el-drawer
      :visible.sync="isDrawer"
      :direction="direction"
      :size="this.$store.state.user.userIsPhone ? '90%' : '400px'"
      :with-header="false"
      @close="defaultData"
    >
      <div class="drawer-title">
        <div class="drawer-title">新增字典</div>
        <div class="iconfont icon-guanbi" @click="isDrawer = false"></div>
      </div>
      <div class="head-box">
        <div class="identifier-box"></div>
        <div>基本信息</div>
      </div>
        <el-form :model="formData"  :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item  label="字典关键字" prop="dicKey">
              <el-input v-model="formData.dicKey" style="width: 92%;height: 100%;"></el-input>
            </el-form-item>
            <el-form-item label="字典名称"  prop="dicValue">
              <el-input v-model="formData.dicValue" style="width: 92%;height: 100%;"></el-input>
            </el-form-item>
            <el-form-item label="所属类型"  prop="typeName">
                <el-select v-model="formData.typeName" placeholder="请选择字典类型" filterable allow-create style="width: 92%;">
              <el-option v-for="(item, index) in dicOptions" :key="index" :label="item.label" :value="item.label"></el-option>
            </el-select>
        </el-form-item>
      </el-form>
      <div class="head-box" style="height: 0"></div>
      <div class="btn-box">
        <div class="remove-btn" @click="cancellation('ruleForm')">取消</div>
        <div class="sub-btn" @click="determine('ruleForm')">提交</div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { addDictionaryApi } from '@/api/index'
import cityList2 from '@/config/cityList2'

export default {
  computed: {
    cityList2 () {
      return cityList2
    }
  },
  props: {
    drawer: {
      type: Boolean,
      default: false
    },
    dicOptions: {
      type: Array,
      default: undefined
    },
    getDictionaryList: {
      type: Function,
      default: undefined
    }
  },
  data () {
    return {
      // 是否弹出抽屉
      isDrawer: false,
      // 抽屉弹出方向
      direction: 'rtl',
      formData: {
        // 提交人
        typeName: '',
        // 项目名称
        dicKey: '',
        // 工作类型
        dicValue: ''
      },
      // 项目数据源
      proListOption: [],
      rules: {
        dicKey: [
          { required: true, message: '请输入字典关键字', trigger: 'blur' }
        ],
        dicValue: [
          { required: true, message: '请输入字典名称', trigger: 'blur' }
        ],
        typeName: [
          { required: true, message: '请选择字典类型', trigger: 'blur' }
        ]
      }
    }
  },

  mounted () {
  },

  methods: {
    openDrawer () {
      this.isDrawer = true
      this.formData.sale = this.$store.state.user.userCode
    },

    // 取消新增
    cancellation (formName) {
      this.$refs[formName].resetFields()
    },
    // 确认新增
    async determine (formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          let data = {
            typename: this.formData.typeName,
            dickey: this.formData.dicKey,
            dicvalue: this.formData.dicValue
          }
          console.log('新增---->', data)
          let res = await addDictionaryApi(data)
          if (res === '字典添加成功') {
            this.$message.success('字典提交成功！！！')
            this.resetting()
            if (this.$store.state.user.userIsPhone) {
              this.$router.push('/MobileHomeView')
            } else {
              this.$router.push('/tablePage')
            }
          } else {
            this.$message.success(`${res}！！！`)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
      
    },

    defaultData () {
      this.cancellation('ruleForm')
      this.formData.dicKey = ''
      this.formData.dicValue = ''
      this.formData.typeName = ''
    }
  }
}
</script>

<style lang="scss" scoped>
.add-main-box {
  .drawer-title {
    display: flex;
    justify-content: space-between;
    margin-top: 5px;
    margin-left: 5px;
    padding: 5px;
    padding-right: 15px;
    font-size: 16px;
    > div {
      font-weight: bold;
    }
    .icon-guanbi:hover {
      cursor: pointer;
    }
  }
  .head-box {
    display: flex;
    align-items: center;
    height: 30px;
    margin-top: 10px;
    margin-bottom: 10px;
    padding-left: 20px;
    font-size: 13px;
    line-height: 30px;
    text-align: start;
    border-bottom: 1px solid #dddcdc;
    .identifier-box {
      width: 5px;
      height: 10px;
      margin-right: 6px;
      background: #11a70c;
    }
  }
  .info-box {
    display: flex;
    padding-left: 8%;

    /* border: 1px solid red; */
    >div {
      height: 100%;

      /* border: 1px solid green; */
      >div {
        display: flex;
        align-items: center;
        justify-content: space-around;
        height: 30px;
        margin-top: 10px;
      }
    }
    .left-box {
      width: 25%;
      >div {
        justify-content: flex-end;
        font-size: 12px;
      }
    }
    .right-box {
      width: 60%;
      .file-id-box {
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
    }
  }
  .anne-box {
    display: flex;

    /* border: 1px solid red; */
    width: 100%;
    margin-top: 10px;
    padding-left: 8%;
    .upload-demo {
      /* border: 1px solid red; */
      width: 100%;
      text-align: start;
      .upload-btn {
        color: #000;
        background: rgba($color: #000, $alpha: 0%);
        border: none;
      }
      > ul::-webkit-scrollbar {
        width: 4px;
        height: 8px;
      }
      > ul::-webkit-scrollbar-thumb {
        background: fade(rgb(78 182 214), 60%);
        border-radius: 10px;
        box-shadow: inset 0 0 5px rgb(0 0 0 / 20%);
        opacity: 0.2;
      }
      > ul::-webkit-scrollbar-track {
        background: fade(rgb(78 182 214), 30%);
        border-radius: 0;
        box-shadow: inset 0 0 5px rgb(0 0 0 / 20%);
      }
    }
  }
  .btn-box {
    display: flex;
    justify-content: end;

    /* border: 1px solid #c40f0f; */
    width: 100%;
    margin: 10px 0;
    padding-right: 10px;
    padding-left: 230px;
    > div {
      width: 60px;
      height: 32px;
      margin-left: 10px;
      color: #fff;
      font-size: 12px;
      line-height: 32px;
      text-align: center;
      background: #85ce61;
      border-radius: 5px;
    }
    > div:hover {
      background: rgba($color: #85ce61, $alpha: 80%);
      cursor: pointer;
    }
    .sub-btn {
      color: #fff;
      background: #67c23a;
    }
    .sub-btn:hover {
      color: #fff;
      background: #85ce61;
      border-color: #85ce61;
      cursor: pointer;
    }
    .remove-btn {
      color: #000;
      background: rgba($color: #000, $alpha: 0%);
      border: 1px solid #dcdfe6;
    }
    .remove-btn:hover {
      color: #409eff;
      background-color: #ecf5ff;
      border-color: #c6e2ff;
      cursor: pointer;
    }
  }
}

@media screen and (width <= 768px) {
  .main-box {
    background: #ee0909;
  }
}
</style>